package com.my.thread.simple.pool;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.concurrent.*;

/**
 * 线程池中的异常处理
 * @author timo 2021/3/18
 */
@Slf4j
public class ThreadPoolException {
    @Test
    public void test2() {
//        Executor 线程池顶级类
//        Executors 线程池工具类
        ExecutorService executorService = Executors.newCachedThreadPool();//无核心线程数 非核心无限大  最快 队列为链表 1个
        executorService.execute(new Thread(new Runnable(){
                @Override
                public void run() {
                    log.info("info");
                }
        }));

        Executors.newFixedThreadPool(10);//核心线程10 无非核心线程 中等
        Executors.newSingleThreadExecutor(); //最慢 只有一个线程

        //推荐用法 自定义 执行时会先核心（放满）->队列（放满）->非核心
        ThreadPoolExecutor a = new ThreadPoolExecutor(10,//核心线程
                20,//最大线程-- 20-10 = 10 非核心线程数为10
                0L, //时间
                TimeUnit.MILLISECONDS,//单位
                new ArrayBlockingQueue<Runnable>(10)//策略  10个队列数组
        ); //这样设置多大值能有30个线程
// 源码关系  ThreadPoolExecutor（具体实现）-> AbstractExecutorService(合并) ->ExecutorService (提交)->Executor(顶级)
        a.execute(new Runnable() {
            @Override
            public void run() {
                log.info("=====11===112====");
            }
        });
    }

    @Test
    public void test1() throws InterruptedException {
        ExecutorService execute = new ThreadPoolExecutor(
                1,
                1,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                new ThreadFactoryBuilder().setUncaughtExceptionHandler(new MyHandler()).build()
        );
        ThreadPoolExecutor b = new ThreadPoolExecutor(
                1,
                1,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                new ThreadFactoryBuilder().setUncaughtExceptionHandler(new MyHandler()).build()
        );

        execute.execute(new Runnable() {
            @Override
            public void run() {
                log.info("=====11=======");
            }
        });

        execute.execute(new Run1());
        TimeUnit.SECONDS.sleep(5);
        execute.shutdown();
    }
    @Slf4j
    static class Run1 implements Runnable {
        @Override
        public void run() {
            int count = 0;
            while (true) {
                System.out.println(count);
                count++;
                log.info("-------222-------------{}", count);
                if (count == 10) {
                    System.out.println(1 / 0);
//                    try {
//                    } catch (Exception e) {
//                        LOGGER.error("Exception",e);
//                    }
                }
                if (count == 20) {
                    log.info("count={}", count);
                    break;
                }
            }
        }
    }
    @Slf4j
    static class MyHandler implements Thread.UncaughtExceptionHandler {
        //发生异常
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            log.error("threadId = {}, threadName = {}, ex = {}", t.getId(), t.getName(), e.getMessage());
        }
    }
}


